<?php
class ControllerDeployVpc extends Controller {
        public function index(){
            if(isset($this->request->post['app_id'])){
                $this->data['app_id']=$this->request->post['app_id'];
            }else{
                $this->data['app_id']=0;
            }

            $this->data['region']=$this->request->post['region'];
            $this->data['vpc_name']="";
            $this->data['eip_name']="";
            $this->data['bandwidth']="1";

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/vpc.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/deploy/vpc.tpl';
            } else {
                $this->template = 'default/template/deploy/vpc.tpl';
            }
            $this->response->setOutput($this->render());

        }
        public function check_vpc_name_exist(){
            $vpcname = $this->request->post['vpcname'];
            if(!$this->backend->check_vpc_name_exist($this->request->post['app_id'],$vpcname, $this->customer->getEmail(), $this->customer->getPassPlain())){
                $json['result']="success";
            }else{
                $json['result']="exist";
            }
            $this->response->setOutput(json_encode($json));
        }
        public function creat_vpc(){
            $this->language->load('deploy/vpc');

            $json = array();
            // Validate if customer is logged in.
            if (!$this->customer->isLogged()) {
                $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
            }
            // Validate cart has products and has stock.
            if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
                $json['redirect'] = $this->url->link('checkout/cart');
            }
            // Validate VPC Name
            if (!$json) {
                if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['vpc_name'])) {
                    $json['error']['vpc_name'] = $this->language->get('error_vpc_name');
                }

                if(is_numeric($this->request->post['bandwidth'])&&$this->request->post['bandwidth']>0&&$this->request->post['bandwidth']<301){

                }else{
                    $json['error']['bandwidth'] = $this->language->get('error_bandwidth');
                }

                if(isset($this->session->data['app']['vpc'][$this->request->post['app_id']]['vpc_name'])&&$this->request->post['vpc_name']!=$this->session->data['app']['vpc'][$this->request->post['app_id']]['vpc_name']){
                if($this->backend->check_vpc_name_exist($this->request->post['app_id'],$this->request->post['vpc_name'], $this->customer->getEmail(), $this->customer->getPassPlain())){
                    $json['error']['vpc_name'] = $this->language->get('error_vpc_name_exist');
                }
                }
            }
            // Update session
            if (!$json) {
                $eip_name="eip_".substr(md5(uniqid(rand(), true)), 0, 9);
                $this->session->data['app']['vpc'][$this->request->post['app_id']]=array(
                    //'cloud_zone'  =>$this->session->data['app']['basic']['region'],
                    'region'  =>trim($this->request->post['region']),
                    'vpc_name'  => trim($this->request->post['vpc_name']),
                    'eip_name'  =>$eip_name ,
                    'bandwidth'  => trim($this->request->post['bandwidth']),
                );
                $json="succsess";
            }
            $this->response->setOutput(json_encode($json));
    }
        public function edit_vpc(){
        $this->language->load('deploy/vpc');

        $json = array();
        // Validate if customer is logged in.
        if (!$this->customer->isLogged()) {
            $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
        }
        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
            $json['redirect'] = $this->url->link('checkout/cart');
        }
        // Validate VPC Name
        if (!$json) {
            if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['vpc_name'])) {
                $json['error']['vpc_name'] = $this->language->get('error_vpc_name');
            }

            if(is_numeric($this->request->post['bandwidth'])&&$this->request->post['bandwidth']>0&&$this->request->post['bandwidth']<301){

            }else{
                $json['error']['bandwidth'] = $this->language->get('error_bandwidth');
            }

            if($this->backend->check_vpc_name_exist($this->request->post['app_id'],$this->request->post['vpc_name'], $this->customer->getEmail(), $this->customer->getPassPlain())){
                $json['error']['vpc_name'] = $this->language->get('error_vpc_name_exist');
            }
        }
        // Update session
        if (!$json) {
            $eip_name="eip_".trim($this->request->post['vpc_name']);
            $this->session->data['app']['vpc'][$this->request->post['app_id']]=array(
                //'cloud_zone'  =>$this->session->data['app']['basic']['region'],
                 'region'  =>trim($this->request->post['region']),
                'vpc_name'  => trim($this->request->post['vpc_name']),
                'eip_name'  =>$eip_name ,
                'bandwidth'  => trim($this->request->post['bandwidth']),
            );
            $json="succsess";
        }
        $this->response->setOutput(json_encode($json));
    }

        public function edit(){
            if(isset($this->request->post['app_id'])){
               $app_id=$this->request->post['app_id'];
            }else{
               $app_id=0;
            }

            $this->data['app_id'] =$app_id;
            $this->data['edit'] =true;;
            $this->data['region']=$this->session->data['app']['vpc'][$app_id]['region'];
            $this->data['vpc_name']=$this->session->data['app']['vpc'][$app_id]['vpc_name'];
            $this->data['eip_name']=$this->session->data['app']['vpc'][$app_id]['eip_name'];
            $this->data['bandwidth']=$this->session->data['app']['vpc'][$app_id]['bandwidth'];
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/vpc.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/deploy/vpc.tpl';
            } else {
                $this->template = 'default/template/deploy/vpc.tpl';
            }
            $this->response->setOutput($this->render());

        }
}
